<?php
class action extends mysql {
	var $available_throw_num;
	var $user_data_table;
	var $bottle_table;
	var $bottle_pool_table;
	var $message_table;
	var $user_bottle_table;
	var $user_info_table;
	
	/*构造函数*/
	function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding, $db_tag) {
		parent::__construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding, $db_tag);
		
		$this->available_throw_num = "available_throw_times";
		$this->user_data_table = "user_data";
		$this->bottle_table = "bottle";
		$this->bottle_pool_table = "bottle_pool";
		$this->message_table = "message";
		$this->user_bottle_table = "user_bottle";
		$this->user_info_table = "user_info";
	}
	
	
	// 扔一个新瓶子的操作集成
	public function throw_new_bottle($user_id, $message){
		
		//------- 先检查是否有这个用户存在
		if( 0 == $this->check_user_exist($user_id)){
			return false;
		}
		
		// 先检查是否有可用的瓶子
		$available_throw_time = $this->get_available_throw_time($user_id);
		if($available_throw_time==null || $available_throw_time == 0){
			return false;
		}
		
		//-------增加一个瓶子
		$bottle_id = $this->add_bottle($user_id, $user_id, $message);
		print_r("bottle id is :$bottle_id");
		if($bottle_id==0){
			return false;
		}
		
		// ---------池子中增加一个瓶子
		$this->add_bottle_to_pool($bottle_id);
		
		// 增加一个message
		$this->add_message($bottle_id, $user_id, $message);
		
		// -------用户的瓶子关联上
		$this->bind_bottle_to_user($bottle_id, $user_id,0, 1);
		
		// --------用户的可扔瓶子减少，积分增加
		$available_throw_time = $available_throw_time -1;
		if($available_throw_time<0){
			$available_throw_time = 0;
		}
		$credits = $this->get_credits($user_id);
		$credits += $default_credits_add;
		$this->update_user_data($user_id, $available_throw_time, $credits);
		
		return true;
	}
	
	// 扔一个旧瓶子的操作,已经存在的瓶子
	public function throw_old_bottle($user_id, $bottle_id, $message){
		
		//------- 先检查是否有这个用户存在
		if( 0 == $this->check_user_exist($user_id)){
			return false;
		}
		//------- 先检查瓶子存在
		if( 0 == $this->check_bottle_exist($bottle_id)){
			return false;
		}

		// 增加一个message
		$this->add_message($bottle_id, $user_id, $message);
		
		// --------用户积分增加
		$credits = $this->get_credits($user_id);
		$credits += $default_credits_add;
		$this->set_credits($user_id, $credits);
	}
	
	
	// 用户第一次登陆或者使用的时候的初始化操作
	public function user_login($user_id, $nickname, $sex, $location="" ) {
		// 先检查是否有这个用户存在
		if( 1 == $this->check_user_exist($user_id)){
			// 更新登陆时间，计算可用瓶子数
			
			return false;
		}
		
		// if not exist,create it
		$this->create_user($user_id,$nickname, $sex, $location="");
	}

	// 获取用户的一些数据，通过引用传值
	public function get_user_data($user_id, &$available_throw_times, &$available_pick_times,&$credits){
		$table = $this->user_data_table;
		$this->select($table, "available_throw_times,available_pick_times, credits", "user_id=$user_id");
		
		$row = $this->fetch_array();
		if("$row[0]" !=""){
			
			$available_throw_times = $row['available_throw_times'];
			$available_pick_times  = $row['available_pick_times'];
			$credits			   = $row['credits'];

			return true;
		}
		return false;
	}
	
	
	// 从海里捡一个瓶子, return : bottle_id
	public function pickup_bottle($user_id){
		//------- 先检查是否有这个用户存在
		if( 0 == $this->check_user_exist($user_id)){
			return -1;
		}
		
		// 从bottle_pool 中随机取得一个瓶子，或者不随机，通过一个函数映射。返回bottle_id
		$id = 0;
		$bottle_id = 0;
		
		if($this->getbottle_from_bottlpool($id, $bottle_id)){
			// delete 
			$this->remove_bottle_from_pool($id);
			return $bottle_id;
		}
		else{
			// get failed
			return -1;
		}
	}
	
	// 获取一个瓶子的信息
	public function get_bottle_info($bottle_id){
		
		// 获取瓶子中的message
		$table = $this->message_table;
		$this->select($table, "author_id, message, time", "bottle_id=$bottle_id");
		$i = 0;
		$list = array();
		while($row = $this->fetch_array()){
			$list[$i]['author_id'] 	= $row['author_id'];
			$list[$i]['message'] 	= $row['message'];
			$list[$i]['time'] 		= $row['time'];
			$i++;
		}
		return $list;
		
	}
	
	
	
	
	
	
	
	
	//------------------------------------------------
	
	// 获取user的可以扔的瓶子的个数
	private function get_available_throw_time($user_id){
		// 看user是否有足够的瓶子
		$table = $this->user_data_table;
		$this->select($table, $this->available_throw_num, "user_id=$user_id");
		$row = $this->fetch_array();
		if("$row[0]" !=""){
			return "$row[0]";
		}
		return null;
	}
	
	// 增加一个瓶子到bottle表
	private function add_bottle($sender_id, $last_response_id, $last_message, $type="0", $status="0"){
		$table = $this->bottle_table;
		$this->insert($table, "sender_id,last_response_id,last_message,type,status", 
					"$sender_id,$last_response_id,$last_message,$type,$status") ;
		return $this->insert_id();
	}
	
	
	
	// pool 中增加一个瓶子
	private function add_bottle_to_pool($bottle_id){
		$table = $this->bottle_pool_table;
		$this->insert($table, "bottle_id", "$bottle_id") ;
		return $this->insert_id();
	}
	
	// 添加一条信息
	private function add_message($bottle_id, $author_id, $message){
		$table = $this->message_table;
		$time = date('Y-m-d H:i:s');
		$this->insert($table, "bottle_id, author_id, message, time", 
					"$bottle_id, $author_id, $message,'$time'");
		return $this->insert_id();
	}
	
	
	// 将瓶子绑到某个user上
	private function bind_bottle_to_user($bottle_id, $user_id,$type="0", $is_readed="0"){
		$table = $this->user_bottle_table;
		$this->insert($table, "user_id, bottle_id, type, is_readed", 
					"$user_id, $bottle_id, $type, $is_readed");
		return $this->insert_id();
	}
	
	// 获取用户积分
	private function get_credits($user_id){
		$col_name = "credits";
		$table = $this->user_data_table;
		
		$this->select($table, $col_name, "user_id=$user_id");
		$row = $this->fetch_array();
		if("$row[0]" !=""){
			return "$row[0]";
		}
		return 0;
	}
	
	// 设置用户积分
	private function set_credits($user_id, $credits){
		$table = $this->user_data_table;
		$this->update($table, "credits=$credits", "user_id=$user_id");
	}
	
	
	// 更新user_data
	private function update_user_data($user_id, $available_throw_times, $credits){
		$table = $this->user_data_table;
		
		$this->update($table, "available_throw_times=$available_throw_times,credits=$credits", "user_id=$user_id");
		
	}
	
	// 检查用户是否存在，存在返回1.否则返回0
	private function check_user_exist($user_id){
		$table = $this->user_info_table;
		$col_name = "id";
			
		$this->select($table, $col_name, "user_id=$user_id");
		$row = $this->fetch_array();
		if("$row[0]" !=""){
			return 1;
		}
		return 0;
	}
	
	// 检查瓶子是否存在，存在返回1.否则返回0
	private function check_bottle_exist($bottle_id){
		$table = $this->bottle_table;
		$col_name = "sender_id";
			
		$this->select($table, $col_name, "bottle_id=$bottle_id");
		
		$row = $this->fetch_array();
		if("$row[0]" !=""){
			return 1;
		}
		return 0;
	}
	
	// create user when user first time use
	private function create_user($user_id, $nickname, $sex, $location="" ){
		
		$table = $this->user_info_table;
		$this->insert($table, "user_id, nickname, loaction, sex", 
					"$user_id, $nickname, $location, $sex");
					
		$table = $this->user_data_table;
		$available_throw_times = $default_throw_times;
		$available_pick_times = $default_pick_times;
		$time = date('Y-m-d H:i:s');
		$this->insert($table, "user_id, available_throw_times, available_pick_times,credits, last_login_time", 
					"$user_id, $available_throw_times, $available_pick_times,0, $time");
		
	}
	
	// 从bottle_pool中获取一个瓶子,引用传值，返回true，false
	private function getbottle_from_bottlpool(&$id, &$bottle_id){
		$table = $this->bottle_pool_table;
		
		$sql = "select id from $table order by id LIMIT 100";
		$ret = $this->query($sql);
		
		if(!$ret){
			if($this->show_error){
				$this->show_error("select error",$sql);
			}
		}
		
		$row_num = $this->db_num_rows();
		$rand_id = rand(0,$row_num);
		$this->db_data_seek($rand_id);
		$row = $this->fetch_row();
		
		if("$row[0]" !=""){
			$id = $row['id'];
			$bottle_id = $row['bottle_id'];
			return true;
		}
		return false;
	}
	
	// 从bottle_pool中删除bottle
	private function remove_bottle_from_pool($id){
		$table = $this->bottle_pool_table;
		$this->delete($table, "id=$id");
	}
	
	
} //end class

?>